/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "GimboSettings.hpp"
#include "settings.hpp"

TGimboSettings::TGimboSettings()
  :Autoconnect(false),
  Hub("asrobo.hut.fi"),
  Port(40002),
  OwnName(""),
  Loop1Enabled(true),
  Loop2Enabled(true),
  Loop1Timeout(1000),
  Loop2Timeout(5000),
  ComponentsDirectory("./components"),
  AutoTerminateComponents(true)
{
  Hubs.push_back("asrobo.hut.fi");
  Hubs.push_back("rousku.dyndns.tv");
  Hubs.push_back("hydpc9.me.tut.fi");
  Hubs.push_back("lousku.dyndns.tv");
  Hubs.push_back("localhost");
}

bool TGimboSettings::SaveXMLFile(const std::string &aXmlFilename, 
                                 const std::string &aRootKey)
{
  bool result = false;

  gim::CSettings s;
  
  //write stuff
  s.SetValue(aRootKey+".Autoconnect", Autoconnect);
  s.SetValue(aRootKey+".Hub", Hub);
  
  std::string hubsList;
  std::vector<std::string>::iterator i;
  for (i=Hubs.begin();i!=Hubs.end();i++)
  {
    hubsList.append(*i);
    hubsList.append(" ");
  }
  s.SetValue(aRootKey+".Hubs", hubsList);
  s.SetValue(aRootKey+".Port", Port);
  s.SetValue(aRootKey+".OwnName", OwnName);
  s.SetValue(aRootKey+".Loop1Enabled", Loop1Enabled);
  s.SetValue(aRootKey+".Loop2Enabled", Loop2Enabled);
  s.SetValue(aRootKey+".Loop1Timeout", Loop1Timeout);
  s.SetValue(aRootKey+".Loop2Timeout", Loop2Timeout);
  
  s.SetValue(aRootKey+".ComponentsDirectory", ComponentsDirectory);
  s.SetValue(aRootKey+".AutoTerminateComponents", AutoTerminateComponents);
  
  result = s.WriteSettingsXMLFile(aXmlFilename);

  return result;
}

bool TGimboSettings::LoadXMLFile(const std::string &aXmlFilename, 
                                 const std::string &aRootKey)
{
  bool result = false;
  gim::CSettings s;
  
  if (s.ReadSettingsXMLFile(aXmlFilename)) {
    result = true;
    
    int t;
    std::string str;
    
    s.SetBaseKey(aRootKey);
    
    //read stuff
    if (s.GetValue("Autoconnect", t)) {
      Autoconnect = (t != 0);
    }
    if (s.GetValue("Hub", str)) {
      Hub=str;
    }
    
    if (s.GetValue("Hubs", str)) {
      Hubs.clear();
      while (str.length())
      {
        std::string::size_type p=str.find_first_of(" ");
        if (p!=std::string::npos)
        {
          Hubs.push_back(str.substr(0, p));
        } else {
          Hubs.push_back(str);
          break;
        }
        str=str.substr( str.find_first_of(" ")+1 );
      }
    }
    
    
    if (s.GetValue("Port", t)) {
      Port = t;
    }
    if (s.GetValue("OwnName", str)) {
      OwnName = str;
    }
    if (s.GetValue("Loop1Enabled", t)) {
      Loop1Enabled = (t != 0);
    }
    if (s.GetValue("Loop2Enabled", t)) {
      Loop2Enabled = (t != 0);
    }
    if (s.GetValue("Loop1Timeout", t)) {
      Loop1Timeout = t;
    }
    if (s.GetValue("Loop2Timeout", t)) {
      Loop2Timeout = t;
    }
    
    if (s.GetValue("ComponentsDirectory", str)) {
      ComponentsDirectory = str;
    }
    if (s.GetValue("AutoTerminateComponents", t)) {
      AutoTerminateComponents=(t != 0);;
    }
  }
  
  return result;
}

